package com.car.shopping.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.commons.core.model.Result;
import com.car.shopping.controller.form.FindGoodsForPageForm;
import com.car.shopping.controller.form.GoodsTypeForm;
import com.car.shopping.controller.form.ModifyGoodsTypeForm;
import com.car.shopping.entity.GoodsInfo;
import com.car.shopping.entity.GoodsType;
import com.car.shopping.service.GoodsTypeService;
import com.car.shopping.service.param.ModifyGoodsTypeParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@RestController
@RequestMapping("/goodsType")
@Api(tags = "商品类别相关接口")
public class GoodsTypeController {
    @Autowired
    private GoodsTypeService goodsTypeService;
    //查询全部商品类型
    @ApiOperation("查询全部商品类型")
    @GetMapping("/findAllGoodsType.ty")
    public Result findAllGoodsType() throws Exception {
        try {
            List<GoodsType> type = goodsTypeService.findAllGoodsType();
            return Result.getResult().setMsg("查询成功").setCode(200).setData(type);
        } catch (Exception e) {
            return Result.getResult().setMsg("查询失败").setCode(500).setData(null);
        }
    }
    //增加商品类别
    @ApiOperation("增加商品类别")
    @PostMapping("/addGoodsType.pt")
    public Result addGoodsType(@RequestBody GoodsTypeForm form) throws Exception {
        GoodsType type = goodsTypeService.addType(form);
        return Result.getResult().setMsg("新增类别成功").setCode(200).setData(type);
    }
    //删除商品类别
    @ApiOperation("删除商品类别")
    @PostMapping("/delGoodsType.pt")
    public Result delGoodsType(@RequestBody Map<String, Integer> request) throws Exception {
        Integer goodsTypeId = request.get("goodsTypeId");
        if (goodsTypeId == null) {
            return Result.getResult().setMsg("缺少参数 goodsTypeId").setCode(400);
        }
        goodsTypeService.delGoodsType(goodsTypeId);
        return Result.getResult().setMsg("删除类别成功").setCode(200);
    }



    //修改商品类别
    @ApiOperation("修改商品类别")
    @PostMapping("/modifyGoodsType.pt")
    public Result modifyGoodsType(@RequestBody ModifyGoodsTypeForm form) throws Exception {
        goodsTypeService.modifyGoodsType(BeanUtil.toBean(form, ModifyGoodsTypeParam.class));
        return Result.getResult().setMsg("修改商品类别成功").setCode(200);
    }




}

